// $codepro.audit.disable numericLiterals, com.instantiations.assist.eclipse.analysis.unusedReturnValue

/**
 * Class: DOCTOR PANEL
 * Description:
 * 
 */
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JList;


/**
 * @author Team Awesome Express
 * @version $ 1.0 Revision $
 */
public class DoctorPanel extends JPanel {
	/**
	 * Field myDataManager.
	 */
	private final DataManager myDataManager;

	/**
	 * Field viewAppt.
	 */
	private ViewAppointment viewAppt;

	/**
	 * Field treatRecord.
	 */
	private CreateTreatmentRecord treatRecord;

	/**
	 * Field patientList.
	 */
	private JList patientList;

	/**
	 * Field patientArrayList.
	 */
	private final ArrayList patientArrayList = new ArrayList();

	/**
	 * Field patientArray.
	 */
	private Patient[] patientArray;

	/**
	 * Field newPatient.
	 */
	private CreateAccount newPatient;

	/**
	 * Field frame.
	 */
	private static JFrame Frame;

	/**
	 * Create the panel.
	 * @param name String
	 * @param dataManager DataManager
	 */
	public DoctorPanel(String name, DataManager dataManager) {
		// setBackground(new Color(204, 204, 255));
		
		setBackground(GlobalVars.BACKGROUND);
		
		this.myDataManager = dataManager; // Initialize DataManager
		
		final int numPatients = myDataManager.getNumRows(GlobalVars.PATIENT_SHEET) + 1;
		
		patientArray = getPatients(numPatients); // Holds list of patient objects
		
		for (int i = 1; i < patientArray.length; i++) { 
			patientArrayList.add(fillPatientArray(patientArray[i])); // This will put the  first name, and last name into one index of an arraylist to display
		}

		final JButton btnCreateEdit = new JButton("Create New Patient");

		btnCreateEdit.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) { // If the view / edit Patients button is Pressed
				patientButtonPressed(); // will call method that calls initialize table on datamanager
			}

		});
		setLayout(null);
		btnCreateEdit.setBounds(67, 284, 203, 29);
		add(btnCreateEdit);
		
		final JButton btnViewAppointments = new JButton("View Appointments");
		btnViewAppointments.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				viewApptButtonPressed();
			}
		});
		btnViewAppointments.setBounds(282, 284, 203, 29);
		add(btnViewAppointments);
		
		final JButton btnCreateTreatmentRecord = new JButton("Create New Treatment Record");
		
		btnCreateTreatmentRecord.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent arg0) {  // Create Treatment Record button is pressed
				treatmentButtonPressed();
			}
		});

		btnCreateTreatmentRecord.setToolTipText("");
		btnCreateTreatmentRecord.setBounds(67, 325, 203, 29);
		add(btnCreateTreatmentRecord);
		
		final JButton btnReportsAndStatistics = new JButton("Reports and Statistics");
		btnReportsAndStatistics.setBounds(282, 325, 203, 29);
		add(btnReportsAndStatistics);
		
		btnReportsAndStatistics.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e){
				reportsAndStatisticsButtonPressed();
			}
		});
		
		final JLabel welcomeLabel = new JLabel("Welcome Doctor " + name);
		welcomeLabel.setBounds(20, 11, 278, 38);
		add(welcomeLabel);
		
		final JLabel headerLabel = new JLabel("Double click a patient to open patient profile");
		headerLabel.setBounds(157, 71, 222, 38);
		add(headerLabel);
		
		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(103, 120, 335, 132);
		add(scrollPane);
		
		patientList = new JList(patientArrayList.toArray());
		scrollPane.setViewportView(patientList);
		
		patientList.addMouseListener(new MouseAdapter() { // Adds support for double clicking
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					final int index = patientList.locationToIndex(e.getPoint()) + 1;
					PatientInformationPanel.createWindow(myDataManager, patientArray[index]); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
				}
			}
		});
		
		final JButton btnLogOut = new JButton("Log Out");
		btnLogOut.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) { // Log out button is pressed
				Frame.dispose(); // Close Doctor Panel
				LoginFrame.createWindow(); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
			}
		});
		btnLogOut.setBounds(181, 372, 188, 23);
		add(btnLogOut);
		
		final JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0){
				refreshButtonPressed(scrollPane, numPatients);

			}
		});
		btnRefresh.setBounds(6, 154, 91, 29);
		add(btnRefresh);

	}

	/**
	 * Method patientButtonPressed.
	 */
	public void patientButtonPressed() {
		newPatient = new CreateAccount(myDataManager);
		}
	
	/**
	 * Will create a jframe to view the appointments
	 */
	public void viewApptButtonPressed() {
		viewAppt = new ViewAppointment(myDataManager);
	}
	
	/**
	 * Method treatmentButtonPressed.
	 */
	public void treatmentButtonPressed() {
		final int index = patientList.getSelectedIndex() + 1;
		treatRecord = new CreateTreatmentRecord(myDataManager, null, -1, patientArray[index]);
	}
	
	/**
	 * Method refreshButtonPressed.
	 * @param scrollPane JScrollPane
	 * @param numPatients int
	 */
	public void refreshButtonPressed(JScrollPane scrollPane, int numPatients){
		final ArrayList refreshPatientArrayList = new ArrayList();
		final int currentNumPatients = myDataManager.getNumRows(GlobalVars.PATIENT_SHEET) + 1;
		if(currentNumPatients == numPatients){
			patientList.repaint();
			scrollPane.repaint();
			return;
		}
		else{
			patientArray = getPatients(currentNumPatients); // Holds list of patient objects
				
			for (int i = 1; i < patientArray.length; i++) { 
				refreshPatientArrayList.add(fillPatientArray(patientArray[i])); // This will put the  first name, and last name into one index of an arraylist to display
				}
		patientList = new JList(refreshPatientArrayList.toArray()); // Display the arraylist in the NursePanel
		patientList.revalidate();
		scrollPane.revalidate();
		patientList.repaint();
		scrollPane.repaint();
		scrollPane.setViewportView(patientList);
		numPatients = currentNumPatients; // $codepro.audit.disable questionableAssignment
		}
		
	}
	
	/**
	 *  Will return an array list of patients in the given row numbers
	 * @param rowNumbers The row numbers to grab data from in the datamanager
	
	 * @return An array list of patients */
	private Patient[] getPatients(int rowNumbers){
		final Patient[] patientsArray = new Patient[rowNumbers];
		final String[][] rawData = new String[rowNumbers][];
		for (int i = 0; i < rowNumbers; i++){
			rawData[i] = myDataManager.getDataAtRow(
						GlobalVars.PATIENT_SHEET,
						1,
						12,
						i + 1
						 );
			patientsArray[i] = new Patient(rawData[i]); 
		}
		
		return patientsArray;
	}
	
	/**
	 * Will grab login name, name, and lastname to display in patient list
	 * @param p A person Object
	
	 * @return login name, name, and lastname to display in patient list */
	  public String fillPatientArray(Person p) {
		  return p.getName() + " " + p.getLastname();
	  }
	  
		/**
		 * Method ReportsAndStatisticsButtonPressed.
		 */
		private void reportsAndStatisticsButtonPressed(){
			IncomeStatementPanel.createWindow(myDataManager); // $codepro.audit.disable
		}
		
	/**
	 * Method createWindow.
	 * @param name String
	 * @param dataManager DataManager
	
	 * @return JFrame */
	public static JFrame createWindow(String name, DataManager dataManager) {
	    Frame = new JFrame();
		Frame.setSize(580, 440);
		Frame.getContentPane().add(new DoctorPanel(name, dataManager));
		Frame.setTitle("Welcome " + name);
		Frame.setVisible(true);
		Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		return Frame;
		}
}
